import { request } from '@/app/http'
import {
  toPageParam,
  toPageResult,
  toRangeParam,
  toAreaParam
} from 'app/converter'
import { dateToStr } from 'utils'

const PATH = '/company/jobs'

export async function searchJob(params, pageData) {
  const { data } = await request({
    url: PATH,
    method: 'get',
    params: { ...toRangeParam(params), ...toPageParam(pageData) }
  })
  return toPageResult(data)
}

export function addJob(data) {
  return request({
    url: PATH,
    method: 'post',
    data: { ...toAreaParam(data), dueDate: dateToStr(data.dueDate) },
    showLoading: true,
    showMsg: true
  })
}

export function updateJob(data) {
  return request({
    url: PATH,
    method: 'put',
    data: { ...toAreaParam(data), dueDate: dateToStr(data.dueDate) },
    showLoading: true,
    showMsg: true
  })
}

export function removeJob(data) {
  return request({
    url: PATH,
    method: 'delete',
    data,
    showLoading: true,
    showMsg: true
  })
}

export function downloadJob(params) {
  return request({
    url: PATH + '/download',
    method: 'get',
    params,
    responseType: 'blob',
    showLoading: true,
    showMsg: true
  })
}

export function approvalJob(data) {
  return request({
    url: PATH + '/approval',
    method: 'post',
    data,
    showLoading: true,
    showMsg: true
  })
}

export function getJobDetail(params) {
  return request({
    url: PATH + '/getDetailById',
    method: 'get',
    params
  })
}

export async function searchJobPost(params, pageData) {
  const { data } = await request({
    url: PATH + '/queryJobPost',
    method: 'get',
    params: { ...toRangeParam(params), ...toPageParam(pageData) }
  })
  return toPageResult(data)
}

export function downloadJobPost(params) {
  return request({
    url: PATH + '/downloadJobPost',
    method: 'get',
    params,
    responseType: 'blob',
    showLoading: true,
    showMsg: true
  })
}

export async function searchJobStat(params, pageData) {
  const { data } = await request({
    url: PATH + '/queryJobStat',
    method: 'get',
    params: { ...toRangeParam(params), ...toPageParam(pageData) }
  })
  return toPageResult(data)
}

export function downloadJobStat(params) {
  return request({
    url: PATH + '/downloadJobStat',
    method: 'get',
    params,
    responseType: 'blob',
    showLoading: true,
    showMsg: true
  })
}

export function getResumeDetail(params) {
  return request({
    url: PATH + '/openResume',
    method: 'get',
    params,
    showLoading: true,
    showMsg: true
  })
}

export function downloadResume(params) {
  return request({
    url: PATH + '/exportResume',
    method: 'get',
    params,
    responseType: 'blob',
    responseFileType: 'zip',
    showLoading: true,
    showMsg: true
  })
}

export function employResume(params) {
  return request({
    url: PATH + '/employ',
    method: 'get',
    params,
    showLoading: true,
    showMsg: true
  })
}
